package com.edu.fd2130sprbootdemo.controller;

import com.edu.fd2130sprbootdemo.bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;

/**
 * @program: fd2130sprbootdemo
 * @description:
 * @author: Teacher.zhang
 * @create: 2022-03-15 10:04
 **/
@Controller
public class ThymeleafController {
    @RequestMapping("/hello")
    public String hello(ModelMap modelMap) throws Exception {
        modelMap.put("welcome", "Welcome to fd2135xxxxxxxx");
        return "hello";
    }

    @RequestMapping("/getUser")
    public String getUser(ModelMap modelMap)throws Exception {
        User user = new User();
        user.setUsername("张三");
        user.setAddress("深圳宝安");
        modelMap.put("user", user);
        return "user";
    }

    @RequestMapping("/getUserBySession")
    public String getUserBySession(ModelMap modelMap, HttpSession session)throws Exception {
        User user = new User();
        user.setUsername("李四");
        user.setAddress("深圳南山");
        session.setAttribute("loginUser",user);
        return "userbysession";
    }

    @RequestMapping("/getUserList")
    public String getUserList(String id,ModelMap modelMap) throws Exception {
        final List<User> userList = Arrays.asList(
                new User("张三","shenzhen"),
                new User("lisi","baoan"),
                new User("wangwu","fenda"));
        modelMap.put("users", userList);
        return "user";
    }
}
